using AudioStudio;
using GameFramework.Battle.Core;
using Gameplay.PVE.Config;
using Gameplay.PVE.Effect;
using Gameplay.PVE.Entity;
using Gameplay.PVE.Utils;
using UnityEngine;
using Yoozoo.Managers;

namespace Gameplay.PVE.Skill
{
    public class SkillInitiative : SkillBase
    {
        private SkillInitiativeMover mover;


        protected override void Reset()
        {
            base.Reset();
            if (mover != null)
            {
                ClassManager.Free(mover);
                mover = null;
            }
        }

        protected override void Start()
        {
            base.Start();
            if (mover == null)
            {
                if (config.cast_type == 5)
                {
                    mover = ClassManager.Get<TrackMover>();
                }
                else if (config.cast_type == 6)
                {
                    mover = ClassManager.Get<SpecialMover>();
                }
                mover?.Init(unit, this);
            }

            hasWeaponDone = false;
            unit.Data.manualForward = Vector3.zero;
            unit.Data.moveState = 0;
            unit.Data.SetIsInMainSkill(true);
        }
        
        protected override void Cast()
        {
            if (mover != null)
            {
                mover.Start(unit.Data.position,targetPosition);
                //unit.Play("Run");
            }
            else if (config.action_name != null)
            {
                unit.Play(config.action_name);
            }
            else
            {
                unit.Play("Attack");
            }
            base.Cast();
        }
        
        protected override bool CastEndCondition()
        {
            bool isMoverFinish = true;
            if (mover != null)
            {
                isMoverFinish = mover.backFinished && mover.moveFinished;
            }
            var isEnd = isMoverFinish && currentTime - stateStartTime >= config.attack_cast_time;
            return isEnd;
        }

        protected override void Storage()
        {
            base.Storage();
            unit.Data.SetIsCanMove(false);
            if (config.storage_name != null)
            {
                unit.Play(config.storage_name);
            }
            else if (config.cast_type == 0)
            {
                unit.Play("Idle");
            }

            unit.Data.forward = unit.Data.targetForward;
            //显示预警
            /*if (unit.Data.team == 2)
            {
                var arg = ClassManager.Get<SkillDirectorMoveArg>();
                arg.position = targetPosition;
                unit.Transmit(ETransmitType.ShowDirectorAlert,arg);
                ClassManager.Free(arg);
            }*/
        }

        protected override void StartRecover()
        {
            base.StartRecover();
            if (!string.IsNullOrEmpty(config.recover_name))
            {
                unit.Play(config.recover_name);
            }
            mover?.Recover();
        }

        protected override void Recover()
        {
            base.Recover();
            unit.Play("Idle",1);
            unit.Data.SetIsInSkill(false);
            unit.Data.SetIsInAttack(false);
            unit.Data.SetIsCanMove(true);
            OnStop();
        }

        protected override void OnStop()
        {
            base.OnStop();
            mover?.End();
            unit.Data.SetIsInSkill(false);
            unit.Data.SetIsInMainSkill(false);
            unit.Data.moveState = 0;
            unit.Transmit(ETransmitType.AttackEnd);
            unit.Transmit(ETransmitType.OnInitiativeSkillEnd);
        }
        
        protected override void RecoverEvent()
        {
            /*if (mover == null)
            {
                 unit.Play("Idle",1);
            }*/
        }

        public override void End()
        {
            base.End();
            //移除预警
            if (unit.Data.team == 2)
            {
                unit.Transmit(ETransmitType.HideDirectorAlert);
            }
        }
        
        protected override bool CheckIsCanSkill()
        {
            return base.CheckIsCanSkill() && unit.Data.IsInSkill() && unit.Data.IsCanSkill();
        }

        private float lastBulletTime;
        
        protected override void CastEvent()
        {
            if (config.summon_id != 0)//召唤类技能
            {
                UpdateSummonSkill();
            }
            else if (config.weapon_id != 0)//武器类技能
            {
                UpdateWeaponSkill();
            }
            else if (mover != null)//移动类
            {
                UpdateMoveSkill();
            }
            else
            {
                base.CastEvent();
            }
        }

        protected override void OnForceStop()
        {
            base.OnForceStop();
            mover?.ForceStop();
            unit.Play("SkillForceStop");
        }


        private bool hasWeaponDone;
        
        private void UpdateWeaponSkill()
        {
            if (hasWeaponDone)
            {
                return;
            }
            hasWeaponDone = true;
            SwitchWeaponArg arg = ClassManager.Get<SwitchWeaponArg>();
            arg.weaponId = config.weapon_id;
            unit.Transmit(ETransmitType.SwitchWeapon,arg);
            ClassManager.Free(arg);
        }
        
        private void UpdateMoveSkill()
        {
            mover.Update();
        }
        
        public override void DoCastEvent()
        {
            base.CastEvent();
        }

        protected override void UpdateSound(PveSkillConfig config)
        {
            if (this.State == ESkillState.None || this.State == ESkillState.Recover || unit.Data.isDead)
            {
                return;
            }
            if (config.effects == null || (mover != null && !mover.moveFinished))
            {
                effectStartTime = currentTime;
                return;
            }

          
            base.UpdateSound(config);
        }
        
        protected override void UpdateEffect(PveSkillConfig config)
        {
            if (this.State == ESkillState.None || this.State == ESkillState.Recover || unit.Data.isDead)
            {
                return;
            }
            if (config.effects == null || (mover != null && !mover.moveFinished))
            {
                effectStartTime = currentTime;
                return;
            }

          
            var effectStateList = this.effectStateList[config.id];
            for (int i = 0; i < config.effects.Count; i++)
            {
                if (!effectStateList[i] && currentTime - effectStartTime >= config.effects[i].x / 30f)
                {
                    Vector3 position = unit.Data.position;
                    Vector3 forward = unit.Data.forward;
                    Transform parent = null;
                    if (unit.Data.skillEffectPoints.Count > config.effects[i].z)
                    {
                        position = unit.Data.skillEffectPoints[(int)config.effects[i].z].position;
                        var effectConfig = PveUtils.GetRpgEffectConfig((int) config.effects[i].y);
                        if (effectConfig.use_parent_forward == 1)
                        {
                            forward = unit.Data.skillEffectPoints[(int) config.effects[i].z].forward;
                        }

                        if (effectConfig.set_parent == 1)
                        {
                            parent = unit.Data.skillEffectPoints[(int) config.effects[i].z];
                        }
                        //parent = unit.Data.skillEffectPoints[(int) config.effects[i].z];
                        //forward = unit.Data.skillEffectPoints[(int)config.effects[i].z].forward;
                    }
                    int effectId = (int) config.effects[i].y;
                    var effect = PveEffectManager.Instance.PlayEffect2((int)config.effects[i].y,position,forward,0, parent);
                    if (effect != null)
                    {
                        SetEffectLayer(effect,targetLayer);
                        float endTime = 5 + currentTime;
                        var effectconfig = PveUtils.GetRpgEffectConfig(effectId);
                        effectList.Add(new SkillEffectData
                        {
                            effect = effect,
                            effectId = effectId,
                            endTime = endTime,
                            effectController = effect.GetComponent<PveEffectController>(),
                            effectConfig = effectconfig,
                        });
                    }
                    effectStateList[i] = true;
                }
            }
        }
    }
}